﻿using System;
using System.Collections.Generic;
using System.Data.Objects.SqlClient;
using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;

namespace ZBJF.Warranty.BLL
{
    public partial class SellerEmployeeRoleMappingService
    {
        public SellerEmployeeRoleMapping LoadByID(long id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }

        public SellerEmployeeRoleMapping Load(long sellerEmployeeID, long sellerEmployeeRoleID)
        {
            return LoadEntities(o => o.SellerEmployeeID == sellerEmployeeID && o.SellerEmployeeRoleID == sellerEmployeeRoleID).FirstOrDefault();
        }

        public bool AddSellerEmployeeRoleMappings(long sellerEmployeeID, List<long> sellerEmployeeRoleIDs)
        {
            var result = true;
            try
            {
                if (sellerEmployeeID > 0 && sellerEmployeeRoleIDs.Any())
                {
                    for (int i = 0; i < sellerEmployeeRoleIDs.Count; i++)
                    {
                        var sellerEmployeeRoleID = sellerEmployeeRoleIDs[i];
                        if (sellerEmployeeRoleID > 0)
                        {
                            var entity = Load(sellerEmployeeID, sellerEmployeeRoleID);
                            if (entity == null)
                            {
                                result = AddEntities(new SellerEmployeeRoleMapping()
                                {
                                    SellerEmployeeID = sellerEmployeeID,
                                    SellerEmployeeRoleID = sellerEmployeeRoleID
                                }).ID > 0;
                            }
                        }
                    }
                    var toBeDeleteRoleMappings = LoadEntities(o => o.SellerEmployeeID == sellerEmployeeID && !sellerEmployeeRoleIDs.Contains(o.SellerEmployeeRoleID)).ToList();
                    toBeDeleteRoleMappings.ForEach(o => result = DeleteEntities(o));
                }
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        public bool DeleteSellerEmployeeRoleMappings(long sellerEmployeeID)
        {
            var result = true;
            var toBeDeleteRoleMappings = LoadEntities(o => o.SellerEmployeeID == sellerEmployeeID).ToList();
            toBeDeleteRoleMappings.ForEach(o => result = DeleteEntities(o));
            return result;
        }

        public IQueryable<SellerEmployeeRoleMapping> LoadByEmployeeID(long employeeID)
        {
            return LoadEntities(o => o.SellerEmployeeID == employeeID);
        }
        public List<long> GetEmployeeRoleIDs(long employeeID)
        {
            return LoadByEmployeeID(employeeID).Select(o => o.SellerEmployeeRoleID).ToList();
        }
        public IQueryable<SellerEmployeeRoleMapping> GetPagingList(BaseQueryParams model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<SellerEmployeeRoleMapping>();
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                string kw = model.Keyword.ToLower();
                expressionWhere = expressionWhere.And(o => (o.SellerEmployeeRole.RoleCode + SqlFunctions.Space(1) + o.SellerEmployeeRole.RoleCode).ToLower().Contains(kw));
            }
            return LoadPagerEntities<SellerEmployeeRoleMapping>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
    }
}